package pl.msikora.android.lapmagic.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class LapMagicDatabaseHelper extends SQLiteOpenHelper {
	
	private static LapMagicDatabaseHelper helperInstance = null;
	
	private static final String TAG = "LapMagicDatabaseHelper";

	private static final String DB_NAME = "lapmagic.db";
	private static int DB_VERSION = 7;
	
	private Context appContext;
	
	public static LapMagicDatabaseHelper getInstance(Context context) {
		if (helperInstance == null) {
			Log.d(TAG, "Creating the Singleton Database Helper instance");
			helperInstance = new LapMagicDatabaseHelper(
					context.getApplicationContext());
		}
		Log.d(TAG, "Returnign the Database Helper instance");
		return helperInstance;
	}

	private LapMagicDatabaseHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		Log.d(TAG, 
				"Setting the Singleton Database Helper application context = " 
				+ context);
		appContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		TracksTable.onCreate(db);
		SessionsTable.onCreate(db);
		PointsTable.onCreate(db);
		LapsTable.onCreate(db);
		SplitsTable.onCreate(db);
		LocationsTable.onCreate(db);
		Triggers.onCreate(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		TracksTable.onUpgrade(db, oldVersion, newVersion);
		SessionsTable.onUpgrade(db, oldVersion, newVersion);
		PointsTable.onUpgrade(db, oldVersion, newVersion);
		LapsTable.onUpgrade(db, oldVersion, newVersion);
		SplitsTable.onUpgrade(db, oldVersion, newVersion);
		LocationsTable.onUpgrade(db, oldVersion, newVersion);
		Triggers.onCreate(db);
	}

}
